package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.controller.InputCheck;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;
import fr.pud.client.model.PUDStation;
import fr.pud.client.view.AddVehicleFrame;
import fr.pud.client.view.MainFrame;

/**
 * Controller which enables to add a vehicle to the fleet (singleton)
 * Apr 8, 2012 - OSE
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         AddVehicleToFleet.java
 */
public class OpenFrameAddVehicle extends AbstractAction {
    /**
     * The serial UID.
     */
    private static final long serialVersionUID        = 1876753189467824070L;
    /**
     * The value associated if the capacity respects the format
     */
    public static final int   CAPACITY_IS_RIGHT       = 1;
    /**
     * The value associated if the drive duration respects the format
     */
    public static final int   DRIVE_DURATION_IS_RIGHT = 2;

    public static boolean checkInputUser(String capacity,
            String maxDriveduration, PUDStation startingPoint,
            PUDStation endingPoint) {
        int result = 0;
        result += capacity.matches(InputCheck.NUMBER_ONLY) ? 0
                : CAPACITY_IS_RIGHT;
        result += maxDriveduration.matches(InputCheck.NUMBER_ONLY) ? 0
                : DRIVE_DURATION_IS_RIGHT;
        switch (result) {
            case 0:
                return true;
            case 1:
                JOptionPane
                        .showMessageDialog(
                                AddVehicleFrame.getInstance(),
                                LanguagesConstants.LANGUAGE
                                        .getString(LanguagesConstants.VEHICLE_CAPACITY)
                                        + " "
                                        + LanguagesConstants.LANGUAGE
                                                .getString(LanguagesConstants.IS_NOT_A_NUMBER),
                                LanguagesConstants.INPUT_ERROR,
                                JOptionPane.ERROR_MESSAGE);
                return false;
            case 2:
                JOptionPane
                        .showMessageDialog(
                                AddVehicleFrame.getInstance(),
                                LanguagesConstants.LANGUAGE
                                        .getString(LanguagesConstants.MAX_DRIVE_DURATION)
                                        + " "
                                        + LanguagesConstants.LANGUAGE
                                                .getString(LanguagesConstants.IS_NOT_A_NUMBER),
                                LanguagesConstants.INPUT_ERROR,
                                JOptionPane.ERROR_MESSAGE);
                return false;
            case 3:
                JOptionPane
                        .showMessageDialog(
                                AddVehicleFrame.getInstance(),
                                LanguagesConstants.LANGUAGE
                                        .getString(LanguagesConstants.VEHICLE_CAPACITY)
                                        + " & "
                                        + LanguagesConstants.LANGUAGE
                                                .getString(LanguagesConstants.MAX_DRIVE_DURATION)
                                        + " "
                                        + LanguagesConstants.LANGUAGE
                                                .getString(LanguagesConstants.ARE_NOT_NUMBERS),
                                LanguagesConstants.INPUT_ERROR,
                                JOptionPane.ERROR_MESSAGE);
                return false;
            default:
                return false;
        }
    }

    /**
     * The default constructor to set the GUI up.
     */
    public OpenFrameAddVehicle() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_VEHICLE));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_VEHICLE_DESCRIPTION));
        this.putValue(SMALL_ICON, ImagesDatabase.TRUCK);
    }

    /**
     * The action when the corresponding button is pressed.
     * If the size is equal to zero, the buttons modify & delete are disabled.
     * Else the button are enabled.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        AddVehicleFrame.getInstance().setVisible(true);
        MainFrame.getMainFrame().setEnabled(false);
        if (Fleet.getInstance().getVehicles().size() == 0) {
            AddVehicleFrame.getInstance().getModify().setVisible(false);
            AddVehicleFrame.getInstance().getDelete().setVisible(false);
        }
        else {
            AddVehicleFrame.getInstance().getModify().setVisible(true);
            AddVehicleFrame.getInstance().getDelete().setVisible(true);
        }
    }
}
